<!-- 奖品管理 - 编辑奖品 -->
<template>
    <div class="rightCenter">
        <div class="editTop">
            <div class="editTopTitle" @click="cancelClick()">奖品管理</div>
            <i class="icon font_family icon-yema-you"></i>
            <div class="editTopTitle">{{editVal}}</div>
        </div>

        <div class="editList">
            <div class="editListName">
                奖品类型：
            </div>
            <div class="editListInpFont">
                <el-select v-model="typeVal" placeholder="请选择">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <span class="warn" v-show='titleShow'>请输入标题</span>
        </div>
        <div class="editList">
            <div class="editListName">
                奖品名称：
            </div>
            <input class="editListInpBox fontBlue" v-model.trim='titleVal' type="text" placeholder="标题不超过50个字" maxlength="50">
            <span class="warn" v-show='titleShow'>请输入标题</span>
        </div>
        <div class="editList">
            <div class="editListName">
                头图：
            </div>
            <div class="editUpImg">
                <div>
                    <div class="editUpImgHand" v-show='!handImgVal'>
                        <i class="icon font_family icon-shangchuanzhaopian"></i>
                    </div>
                    <img id="showImg" class="editHandImg" v-show='handImgVal' :src="handImgVal" alt="">
                </div>
                <div>
                    <label for="fileUpImg">
                        上传
                    </label>
                    <input id="fileUpImg" accept="image/jpeg,image/jpg,image/png,image/bmp,image/gif" type="file" v-on:change="upImgChange($event)">
                    <p :class="{fontGary:!imgFontShow,fontRed:imgFontShow}">（图片大小660*510像素）</p>
                </div>
                <div class="warn" v-show='imgupShow'>
                    请上传头图
                </div>
            </div>
            <span class="warn" v-show='titleShow'>请输入标题</span>
        </div>
        <!-- <div class="editList">
            <div class="editListName">
            </div>
            <ul class="checkUl">
                <li class="checkLi">
                    <div class="checkBox">
                        <div :class="{checkBack:!addGoldShow,checkOkBack:addGoldShow}" @click='addGoldClick(true)'>
                            <div v-show='addGoldShow' class="checkBor"></div>
                        </div>
                        通用增金
                    </div>
                </li>
                <li class="checkLi">
                    <div class="checkBox">
                        <div :class="{checkBack:addGoldShow,checkOkBack:!addGoldShow}" @click='addGoldClick(false)'>
                            <div v-show='!addGoldShow' class="checkBor"></div>
                        </div>
                        点播增金
                    </div>
                </li>
            </ul>
        </div> -->
        <div class="footButton">
            <div class="addBut" @click='addClick'>
                保 存
            </div>
            <div class="cancelBut" @click='cancelClick'>
                取 消
            </div>
        </div>
    </div>
</template>
<script>
import { mapActions } from "vuex";
import prizeRequest from "../../assets/prize";
import fileRequest from "../../assets/file";
export default {
  data() {
    return {
      editVal: "编辑奖品",
      titleVal: "", //标题
      titleShow: false,
      handImgVal: null, //头图
      imgupShow: false,
      imgFontShow: false, //头图限制文字
      addGoldShow: false, //赠金选择
      typeVal: "1", //类型
      options: [
        { value: "0", label: "实物奖品" },
        { value: "1", label: "通用增金" },
        { value: "2", label: "点播增金" }
      ],
      detailsObj: {},
      idVal: ""
    };
  },
  filters: {},
  created: function() {
    let self = this;
    if (sessionStorage.getItem("TABLIST")) {
      let arr = JSON.parse(sessionStorage.getItem("TABLIST"));
      let routShow = false;
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].name == "奖品管理") {
          routShow = true;
        }
      }
      if (!routShow) {
        this.$router.push({
          path: "/login"
        });
        sessionStorage.clear();
        self.$message("暂无权限");
        return;
      }
    }else{
       this.$router.push({
          path: "/login"
        });
        sessionStorage.clear();
        self.$message("暂无权限");
        return;
    }
    if (self.$route.query.compile) {
      self.dateilsFun();
    }
  },
  watch: {},
  mounted() {},
  methods: {
    async dateilsFun() {
      let self = this;
      let obj = sessionStorage.getItem("prizeOBJ");
      self.detailsObj = JSON.parse(obj);
      self.titleVal = self.detailsObj.name;
      self.typeVal = `${self.detailsObj.type}`;
      // self.options.forEach((e, i) => {
      //   if (self.detailsObj.type == e.value) {
      //     self.typeVal = self.options[i].label;
      //   }
      // });
      self.handImgVal = self.detailsObj.image || "";
      self.idVal = self.detailsObj.id;
    },
    // 返回
    cancelClick() {
      this.$router.push({
        path: "/home/prize"
      });
    },
    //选择图片
    upImgChange(e) {
      let self = this;
      let files = e.target.files || e.dataTransfer.files;
      let fileData = files[0];
      let reader = new FileReader();
      reader.onload = function(e) {
        let data = e.target.result;
        let image = new Image();
        image.onload = function() {
          // if (image.width !== 600 || image.height !== 510) {
          //   self.imgFontShow = true;
          //   self.$message({
          //     type: "info",
          //     message: "上传图片尺寸不符，请重新上传"
          //   });
          //   document.getElementById("fileUpImg").value = "";
          // } else if (fileData.size > 1048576) {
          //   self.imgFontShow = true;
          //   self.$message({
          //     type: "info",
          //     message: "上传图片大小过大，请重新上传"
          //   });
          //   document.getElementById("fileUpImg").value = "";
          // } else {
          //   self.imgFontShow = false;
          //   self.uploadBanner(files[0]);
          // }
          self.uploadBanner(files[0]);
        };
        image.src = data;
      };
      reader.readAsDataURL(fileData);
    },
    //上传banner图片
    async uploadBanner(file) {
      let self = this;
      let formdata = new FormData();
      formdata.append("file", file);
      let res = await fileRequest.getfileUploadFile(formdata);
      if (res.statusCode == 200) {
        self.$message({
          message: "上传图片成功",
          type: "success"
        });
        self.handImgVal = res.data;
        document.getElementById("fileUpImg").value = "";
      } else {
        self.$message(res.statusMessage);
        self.dataList = [];
      }
    },
    //赠金选择
    addGoldClick(show) {
      this.addGoldShow = show;
    },
    //保存
    async addClick() {
      let self = this;
      if (self.titleVal.length > 0) {
        let params = {
          name: self.titleVal,
          type: self.typeVal,
          image: self.handImgVal,
          id: self.idVal
        };
        let res = await prizeRequest.getwebAwardUpdate(params);
        if (res.statusCode == 200) {
          if (self.$route.query.compile) {
            self.$message("修改奖品成功！");
          } else {
            self.$message("添加奖品成功！");
          }
          this.$router.push({
            path: "/home/prize"
          });
        } else {
          self.$message(res.statusMessage);
          self.dataList = [];
        }
      } else {
        self.$message({
          type: "info",
          message: "任务名称必填"
        });
      }
    }
  }
};
</script>
<style scoped>
.editUpImgHand {
  width: 200px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  background: #f2f5f7;
  color: #b2b7c0;
}
.editHandImg {
  width: 200px;
  height: 100px;
}
.editUpImgHand i {
  font-size: 20px;
}

.footButton {
  display: flex;
  width: 700px;
  align-items: center;
  margin-top: 124px;
  margin-left: 230px;
}

.addBut {
  width: 150px;
  height: 50px;
  border-radius: 25px;
  text-align: center;
  line-height: 50px;
  cursor: pointer;
  margin: 0 12px;
  background: #279de2;
  color: white;
}

.cancelBut {
  width: 150px;
  height: 50px;
  border-radius: 25px;
  text-align: center;
  line-height: 50px;
  cursor: pointer;
  margin: 0 12px;
  border: 1px solid #279de2;
  color: #279de2;
}

.checkUl {
  width: 420px;
  min-height: 20px;
  display: flex;
}

.checkLi {
  width: 140px;
  height: 20px;
  overflow: hidden;
}

.checkBox {
  width: 90px;
  height: 20px;
  line-height: 20px;
  margin-right: 50px;
  color: #2f3a5d;
  font-weight: bold;
  display: flex;
  justify-content: space-between;
}
</style>